package com.itheima.web.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;
import com.itheima.web.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet {
    private BrandService brandService = new BrandServiceImpl();

    /**
     * 查询所有内容
     * @param req
     * @param resp
     */
    public void selectAll(HttpServletRequest req, HttpServletResponse resp){
        List<Brand> brands = brandService.selectAll();
        String jsonString = JSON.toJSONString(brands);
        resp.setContentType("text/json;charset=utf-8");
        try {
            resp.getWriter().write(jsonString);
        } catch (IOException e) {
            System.out.println("响应数据失败");
            e.printStackTrace();
        }
    }

    /**
     * 添加一条数据
     * @param req
     * @param resp
     */
    public void add(HttpServletRequest req, HttpServletResponse resp){
        try {
            BufferedReader reader = req.getReader();
            String params = reader.readLine();
            Brand brand = JSON.parseObject(params,Brand.class);
            brandService.add(brand);
            resp.getWriter().write("success");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("servlet文件操作失败");
        }
    }
    public void update(HttpServletRequest req, HttpServletResponse resp){}
    public void delete(HttpServletRequest req, HttpServletResponse resp){}

    /**
     * 批量删除
     * @param req
     * @param resp
     */
    public void deleteByIds(HttpServletRequest req, HttpServletResponse resp){
        try {
            BufferedReader reader = req.getReader();
            String params = reader.readLine();
            int[] ids = JSON.parseObject(params,int[].class);
            brandService.deleteByIds(ids);
            resp.getWriter().write("success");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("servlet文件操作失败");
        }
    }

    public void selectByPage(HttpServletRequest req,HttpServletResponse resp){
        // 由于这个需求只需要接受两个整数,不需要用json,直接放请求头里
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));

        PageBean<Brand> pageBean = brandService.selectByPage(currentPage,pageSize);
        String jsonString = JSON.toJSONString(pageBean);
        resp.setContentType("text/json;charset=utf-8");
        try {
            resp.getWriter().write(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("响应数据文件操作失败");
        }
    }

    public void selectByPAC(HttpServletRequest req,HttpServletResponse resp){
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));

        try {
            req.setCharacterEncoding("utf-8");
            BufferedReader reader = req.getReader();
            String params = reader.readLine();
//            System.out.println(params);
            Brand brand = JSON.parseObject(params,Brand.class);
            PageBean<Brand> pageBean = null;
            if(brand==null){
//                System.out.println("接受不到");
                pageBean = brandService.selectByPage(currentPage,pageSize);
            }else{
                pageBean = brandService.selectByPageAndCondition(currentPage,pageSize,brand);
            }

            String jsonString = JSON.toJSONString(pageBean);
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("响应数据出错");
        }
    }
}
